// pages/business/agentshare.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp();
var sceneres = {},
  sendflag = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreeflag: true,
    agent_info: '',
    get_captcha_flag: false,
    get_captcha_word: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //agent_apply_info
    var that = this;
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
    wx.request({
      method: 'POST',
      data: {
        parent_id: options.id
      },
      url: config.agent_apply_info,
      success: function(response) {
        that.setData({
          parent_id: options.id,
          agent_info: response.data.data
        })
      }
    })
    // shihan.sendRequest(config.agent_apply_info, 'POST', { parent_id: options.id })
    //   .then(function (response) {
    //     that.setData({
    //       agent_info: response.data.data
    //     })
    //   }, function (error) {
    //     console.log(error);
    //   });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value,
      get_captcha_flag: e.detail.value ? true : false
    })
  },
  selectAgree: function() {
    this.setData({
      agreeflag: !this.data.agreeflag
    })
  },
  getCaptcha: function(e) { //captcha
    var that = this, open_id = wx.getStorageSync('open_id');
    if (!that.data.phone) {
      shihan.showToast.call(that, '手机号码不能为空');
      return;
    }
    if (!(/^1(0|1|2|3|4|5|6|7|8|9)\d{9}$/.test(that.data.phone))) {
      shihan.showToast.call(that, '请输入正确的手机号码');
      return;
    }
    if (!open_id) {
      wx.setStorageSync('open_id', that.data.phone); //将手机号作为临时openId
    }
    if (sendflag) {
      wx.request({
        method: 'GET',
        data: {
          'type': 'agent_apply',
          'mobile': that.data.phone,
          'device': wx.getStorageSync('open_id')
        },
        url: config.captcha,
        success: function(response) {
          if (response.data.code == 0) {
            var timer, time = 60, sendflag = false;
            
            shihan.showToast.call(that, '验证码发送成功！');
            clearInterval(timer)
            timer = setInterval(function() {
              time--;
              that.setData({
                get_captcha_word: time + '秒'
              })
              if (time === 0) {
                clearInterval(timer);
                that.setData({
                  get_captcha_flag: true,
                  get_captcha_word: '重新发送'
                })
                sendflag = true
              }
            }, 1000)
          } else {
            sendflag = true;
            shihan.showToast.call(that, response.data.msg);
          }
        }
      })
    }
  },
  formSubmit: function(e) {
    var that = this,
      name = '',
      cardno = '',
      wechatno = '',
      address = '',
      mobile = '',
      captcha = '',
      data = {};

    name = e.detail.value.name;
    cardno = e.detail.value.cardno;
    wechatno = e.detail.value.wechatno;
    address = e.detail.value.address;
    mobile = e.detail.value.mobile;
    captcha = e.detail.value.captcha;
    if (!name) {
      shihan.showToast.call(that, '姓名不能为空');
      return;
    }
    if (!(/^[\u4e00-\u9fa5]{0,6}$/.test(name))) {
      shihan.showToast.call(that, '姓名填写不符合格式');
      return;
    }
    if (!cardno) {
      shihan.showToast.call(that, '身份证号不能为空');
      return;
    }
    if (!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(cardno))) {
      shihan.showToast.call(that, '身份证号格式不正确');
      return;
    }
    if (!wechatno) {
      shihan.showToast.call(that, '微信号不能为空');
      return;
    }
    if (!address) {
      shihan.showToast.call(that, '收货地址不能为空');
      return;
    }
    if (String(address).length > 150) {
      shihan.showToast.call(that, '收货地址格式不正确');
      return;
    }
    if (!mobile) {
      shihan.showToast.call(that, '手机号码不能为空');
      return;
    }
    if (!(/^1(0|1|2|3|4|5|6|7|8|9)\d{9}$/.test(mobile))) {
      shihan.showToast.call(that, '手机号码格式不正确');
      return;
    }
    if (String(captcha).length != 6) {
      shihan.showToast.call(that, '请输入验证码');
      return;
    }
    if (!that.data.agreeflag) {
      shihan.showToast.call(that, '请阅读并接受《团队代理制度》');
      return;
    }
    data['real_name'] = name;
    data['id_card'] = cardno;
    data['wechat'] = wechatno;
    data['address'] = address;
    data['mobile'] = mobile;
    data['captcha'] = captcha;
    data['parent_id'] = that.data.parent_id;
    data['device'] = wx.getStorageSync('open_id');
    wx.request({
      method: 'POST',
      data: data,
      url: config.agent_apply,
      success: function(response) {
        if (response.data.code == 0) {
          shihan.showToast.call(that, '提交成功');
          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/business/index/index',
            })
          }, 1000)
        } else if (response.data.code == '1105') {
          shihan.showToast.call(that, '请输入正确的验证码');
        } else {
          shihan.showToast.call(that, response.data.msg);
          if (response.data.code == '27002') { //您已经是代理商了
            setTimeout(function() {
              wx.redirectTo({
                url: '/pages/business/index/index',
              })
            }, 1000)
          }
        }
      }
    })
  },
  openAgree: function() {
    var url = config.commonmoburl + 'other/agentsystem.html';
    wx.navigateTo({
      url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(url) + '&name='
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})